{% extends 'base.html' %}
{% from 'bootstrap/form.html' import render_form %}

{% block title %}管理分类{% endblock %}

{% block content %}
    <div class="page-header">
        <h1>总分类数:
            <small class="text-muted">{{ categories|length }}</small>
            <span class="float-right"><a class="btn btn-primary btn-sm"
                                         href="{{ url_for('.new_category') }}">创建新的分类</a></span>
        </h1>
    </div>
    {% if categories %}
        <table class="table table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>名称</th>
                <th>文章数</th>
                <th>操作</th>
            </tr>
            </thead>
            {% for category in categories %}
                <tr>
                    <td>{{ loop.index }}</td>
                    <td><a href="{{ url_for('blog.show_category', category_id=category.id) }}">{{ category.name }}</a>
                    </td>
                    <td>{{ category.posts|length }}</td>
                    <td>
                        {% if category.id != 1 %}
                            <a class="btn btn-info btn-sm"
                               href="{{ url_for('.edit_category', category_id=category.id) }}">编辑</a>

                            <form class="inline" method="post"
                                  action="{{ url_for('.delete_category', category_id=category.id) }}">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                                <button type="submit" class="btn btn-danger btn-sm"
                                        onclick="return confirm('您确定删除该分类吗?');">删除
                                </button>
                            </form>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
        <p class="text-muted">提示：删除分类不会删除该分类下的文章。此类别下的文章将移至默认类别。</p>
    {% else %}
        <div class="tip"><h5>暂无分类!</h5></div>
    {% endif %}
{% endblock %}
